## Figure 1: The Rising Importance and Depth of Investment Provisions
## and Dispute Settlement Mechanisms in PTAs

rm(list=ls())
date()

## ---------------------------------------------------------
library(dplyr)
require(gdata)
## ---------------------------------------------------------

## setting the working directory as the current directory
setwd(getwd())

## check/create the folder to put outputs
if (file.exists(file.path(getwd(), "figs"))){
    FIG_DIR <- file.path(getwd(), "figs")
} else {
    dir.create(file.path(file.path(getwd(), "figs")))
    FIG_DIR <- file.path(getwd(), "figs")
}
    
data <- read.xls("MM_ptadata_2013.xlsx", sheet=1, header=T,
                 na.strings=c(".", "-99", "-9"))

sub <- filter(data, agmt !="")
## date for signed
signed <- sub$signed
signed2 <- gsub("\\-[0-9]{2}\\-[0-9]{2}", "", sub$exactsign)
signed[is.na(signed)] <- signed2[is.na(signed)]
sub$signed <- signed
sub <- sub[which(sub$signed!=""),]
sub <- sub[which(!is.na(sub$signed)),]

## date for inforce
idx <- which(is.na(sub$inforce) & is.na(sub$notinforce))
sub$inforce[idx] <- sub$signed[idx]
sub <- sub[which(!is.na(sub$inforce)),]
nrow(sub)

sub <- sub[which(sub$nonrecip!=19),]
nrow(sub)

sub.final <- subset(sub, select=c("agmt", "signed", "inforce", "Invest", "Invest_approx",
                             "DSM", "DSM_approx"))

sub.final <- unique(sub.final)
nrow(sub.final)
agmts <- as.character(unique(sub.final$agmt))
length(agmts)


for(i in 1:length(agmts)){
    agmt.i <- agmts[i]
    sub.i <- sub.final[which(sub.final$agmt == agmt.i),]
    if(nrow(sub.i)==1){
        if(i==1){
            Data <- sub.i
        } else {
            Data <- rbind(Data, sub.i)
        }
    } else {
        tmp <- data.frame(agmt=agmt.i, signed=min(sub.i$signed),
                          inforce = min(sub.i$inforce),
                          Invest = max(sub.i$Invest),
                          Invest_approx = max(sub.i$Invest_approx),
                          DSM = max(sub.i$DSM),
                          DSM_approx = max(sub.i$DSM_approx))
        Data <- rbind(Data, tmp)
    }
}

years <- as.numeric(as.character(Data$signed))
cat5 <- ifelse(years<=1955, 1,
               ifelse(years<=1960, 2,
               ifelse(years<=1965, 3,
               ifelse(years<=1970, 4,
               ifelse(years<=1975, 5,
               ifelse(years<=1980, 6,
               ifelse(years<=1985, 7,
               ifelse(years<=1990, 8,
               ifelse(years<=1995, 9,
               ifelse(years<=2000, 10,
               ifelse(years<=2005, 11,
               ifelse(years<=2010, 12, 13))))))))))))

Data$cat5 <- cat5

for(i in 1:12){
    sub.i <- Data[Data$cat5==i,]
    Invest0 <- length(which(sub.i$Invest==0))
    Invest1 <- length(which(sub.i$Invest==1))
    Invest2 <- length(which(sub.i$Invest==2))

    DSM0 <- length(which(sub.i$DSM==0))
    DSM1 <- length(which(sub.i$DSM==0.5))
    DSM2 <- length(which(sub.i$DSM==1))

    tmp.i <- data.frame(cat5=i, Invest0=Invest0, Invest1=Invest1, Invest2=Invest2,
                        DSM0=DSM0, DSM1=DSM1, DSM2=DSM2, total=nrow(sub.i))
    if(i==1){
        D <- tmp.i
    } else {
        D <- rbind(D, tmp.i)
    }
    
}

addTrans <- function(color,trans)
{
  # This function adds transparancy to a color.
  # Define transparancy with an integer between 0 and 255
  # 0 being fully transparant and 255 being fully visable
  # Works with either color and trans a vector of equal length,
  # or one of the two of length 1.

  if (length(color)!=length(trans)&!any(c(length(color),length(trans))==1)) stop("Vector lengths not correct")
  if (length(color)==1 & length(trans)>1) color <- rep(color,length(trans))
  if (length(trans)==1 & length(color)>1) trans <- rep(trans,length(color))

  num2hex <- function(x)
  {
    hex <- unlist(strsplit("0123456789ABCDEF",split=""))
    return(paste(hex[(x-x%%16)/16+1],hex[x%%16+1],sep=""))
  }
  rgb <- rbind(col2rgb(color),trans)
  res <- paste("#",apply(apply(rgb,2,num2hex),2,paste,collapse=""),sep="")
  return(res)
}


pdf(file="./figs/figure1.pdf",
    width=22, height=10)

par(mfrow=c(1,2))

par(cex.lab=2.5,
    cex.axis=1.5,
    cex.main=3,
    mar=c(7,6,5,2))


barplot(t(D[,2:4]),
        main="Investment Provision",
        xlab="", xaxt="n",
        ylab="Number of Preferential Trade Agreements",
        ylim=c(0,100),
        col=c("gray70", "gray40", "gray10"))

label <- c("Before 1955", "1956-1960", "1961-1965",
           "1966-1970", "1971-1975", "1976-1980", "1981-1985",
           "1986-1990", "1991-1995", "1996-2000", "2001-2005",
           "2006-2010")

axis(side=1, at=seq(from=0.7, by=1.2, length.out=12), labels = FALSE)

text(x=seq(from=0.7, by=1.2, length.out=12)-0.5, y=rep(-8,12), labels = label,
     srt = 45, pos = 1, xpd = TRUE, las=2, cex=1.3)


legend(0, 90, fill=c("gray10", "gray40", "gray70"),
       legend = c("Detailed Investment Provision", "Mention of Investment Provision",
           "No Mention of Investment Provision"),
       bty="n", cex=1.8)


par(cex.lab=2.5,
    cex.axis=1.5,
    cex.main=3,
    mar=c(7,6,5,2))


barplot(t(D[,5:7]),
        main="Dispute Settlement Mechanism",
        xlab="", xaxt="n",
        ylab="",
        ylim=c(0,100),
        col=c("gray70", "gray40", "gray10"))

label <- c("Before 1955", "1956-1960", "1961-1965",
           "1966-1970", "1971-1975", "1976-1980", "1981-1985",
           "1986-1990", "1991-1995", "1996-2000", "2001-2005",
           "2006-2010")

axis(side=1, at=seq(from=0.7, by=1.2, length.out=12), labels = FALSE)

text(x=seq(from=0.7, by=1.2, length.out=12)-0.5, y=rep(-8,12), labels = label,
     srt = 45, pos = 1, xpd = TRUE, las=2, cex=1.3)


legend(0, 90, fill=c("gray10", "gray40", "gray70"),
       legend = c("Strong DSM",
           "Some DSM", "No Mention of DSM"),
       bty="n", cex=1.8)


dev.off()

